package article

import (
	"errors"
	"github.com/astaxie/beego/logs"
	"public_security/databases"
	"public_security/models"
	"public_security/plugins"
	"time"
)

func CreateType(art *models.ArticleType) error {
	if err := databases.DbDefault.Model(&models.ArticleType{}).Create(art).Error; err != nil {
		return err
	}
	return nil
}

func UpdateType(art *models.ArticleType) error {
	if err := databases.DbDefault.Model(&models.ArticleType{}).Where("id=?", art.Id).Update("name", art.Name).Error; err != nil {
		return err
	}
	return nil
}

func RemoveType(art *models.ArticleType) error {
	var count int
	databases.DbDefault.Model(&models.Article{}).Select("count(id)").Where("article_type_id=?", art.Id).Scan(&count)
	if count > 0 {
		return errors.New("该分类有文章使用，不支持删除")
	}
	if err := databases.DbDefault.Model(&models.ArticleType{}).Where("id=?", art.Id).Delete(art).Error; err != nil {
		return err
	}
	return nil
}

func QueryType(art *models.ArticleType) []*models.ArticleType {
	arts := make([]*models.ArticleType, 0)
	databases.DbDefault.Model(arts).Find(&arts)
	return arts
}

func CreateArt(art *models.Article) error {
	var count int
	databases.DbDefault.Model(&models.Article{}).Select("count(id)").Where("article_type_id=?", art.Id).Row().Scan(&count)
	if count == 6 && art.ShowIndex == 8 {
		return errors.New("首页最多支持显示8个")
	}
	if art.CreateTime == "" {
		art.CreateTime = time.Now().Format("2006-01-02 15:04:05")
	}
	if art.RedShow == 2 {
		databases.DbDefault.Model(&models.Article{}).Where("red_show=2").Update("red_show", 1)
	}
	art.Status = 2
	if err := databases.DbDefault.Model(&models.Article{}).Create(art).Error; err != nil {
		return err
	}
	return nil
}

func UpdateArt(art *models.Article) error {
	if art.RedShow == 2 {
		databases.DbDefault.Model(&models.Article{}).Where("red_show=2").Update("red_show", 1)
	}
	if err := databases.DbDefault.Model(&models.Article{}).Where("id=?", art.Id).Updates(map[string]interface{}{
		"title":           art.Title,
		"content":         art.Content,
		"image":           art.Image,
		"file":            art.File,
		"article_type_id": art.ArticleTypeId,
		"show_index":      art.ShowIndex,
		"status":          art.Status,
		"red_show":        art.RedShow,
		"create_time":     art.CreateTime,
	}).Error; err != nil {
		return err
	}
	return nil
}

func RemoveArt(art *models.Article) error {
	if err := databases.DbDefault.Model(&models.Article{}).Where("id=?", art.Id).Delete(art).Error; err != nil {
		return err
	}
	return nil
}

func QueryArt(art *models.Article, page *plugins.ResultsPageInfo) ([]*models.Article, *plugins.ResultsPageInfo) {
	arts := make([]*models.Article, 0)
	db := databases.DbDefault.Model(arts)
	if art.Status != 0 {
		db = db.Where("status=?", art.Status)
	}
	if art.ShowIndex != 0 {
		db = db.Where("show_index=?", art.ShowIndex)
	}
	if art.ArticleTypeId != 0 {
		db = db.Where("article_type_id=?", art.ArticleTypeId)
	}
	if art.Title != "" {
		db = db.Where("title like ?", "%"+art.Title+"%")
	}
	if art.BeginTime != "" {
		db = db.Where("create_time>?", art.BeginTime)
	}
	if art.EndTime != "" {
		db = db.Where("create_time<?", art.EndTime)
	}
	if art.Id != 0 {
		db = db.Where("id=?", art.Id)
	}
	if art.RedShow != 0 {
		db = db.Where("red_show=?", art.RedShow)
	}
	db.Count(&page.Total).Limit(page.PerPage).Offset(page.PerPage * (page.Page - 1)).Preload("ArticleType").Order("red_show desc,create_time desc").Find(&arts)
	for _, j := range arts {
		loc, _ := time.LoadLocation("Local")
		ct, _ := time.ParseInLocation("2006-01-02 15:04:05", j.CreateTime, loc)
		nowTime, _ := time.ParseInLocation("2006-01-02 15:04:05", time.Now().Format("2006-01-02 15:04:05"), loc)
		nowTime1, _ := time.ParseInLocation("2006-01-02", time.Now().Format("2006-01-02"), loc)
		if ct.Unix() <= nowTime.Unix() && ct.Unix() >= nowTime1.AddDate(0, 0, -1).Unix() {
			j.IsNew = 2
		}
	}
	return arts, page
}

func QueryAllArt() []*models.ArticleType {
	result := make([]*models.ArticleType, 0)
	databases.DbDefault.Model(models.ArticleType{}).Find(&result)
	for _, v := range result {
		databases.DbDefault.Model(models.Article{}).Where("article_type_id=? and status=2 and show_index=2", v.Id).Order("red_show desc,create_time desc").Find(&v.Articles)
		s := make([]*models.Article, 0)
		databases.DbDefault.Model(models.Article{}).Where("article_type_id=? and status=2 and show_index!=2", v.Id).Order("red_show desc,create_time desc").Find(&s)
		if len(s) > 0 && len(v.Articles) < 8 {
			for _, k := range s {
				v.Articles = append(v.Articles, k)
			}
		}
		for _, j := range v.Articles {
			loc, _ := time.LoadLocation("Local")
			ct, _ := time.ParseInLocation("2006-01-02 15:04:05", j.CreateTime, loc)
			nowTime, _ := time.ParseInLocation("2006-01-02 15:04:05", time.Now().Format("2006-01-02 15:04:05"), loc)
			nowTime1, _ := time.ParseInLocation("2006-01-02", time.Now().Format("2006-01-02"), loc)
			if ct.Unix() <= nowTime.Unix() && ct.Unix() >= nowTime1.AddDate(0, 0, -1).Unix() {
				logs.Info(j.CreateTime, j.Title)
				j.IsNew = 2
			}
		}
	}
	return result
}
